package com.qianhui.listener;

import com.qianhui.bean.BatchTaskJob_OneDay;
import com.qianhui.bean.BatchTaskJob_OneHour;
import com.qianhui.bean.BatchTaskJob_OneMonth;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Calendar;
import java.util.Date;

public class BatchTaskTimerListener implements ServletContextListener
{

    @Override
    public void contextInitialized(ServletContextEvent arg0)
    {
        SchedulerFactory factory = new StdSchedulerFactory();
        // 从工厂里面拿到一个scheduler实例
        Scheduler scheduler_one_day;
        Scheduler scheduler_one_hour;
        Scheduler scheduler_one_month;
        Scheduler scheduler_half_hour;
        try
        {
            scheduler_one_day = factory.getScheduler();
            Date runTime_one_day = DateBuilder.dateOf(3, 0, 0);

            if (runTime_one_day.before(new Date()))
            {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(runTime_one_day);
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                runTime_one_day = calendar.getTime();
            }
            //Date runTime_one_day = DateBuilder.evenMinuteDateAfterNow();
            JobDetail job_one_day = JobBuilder.newJob(BatchTaskJob_OneDay.class).withIdentity("job1", "group1").build();
            Trigger trigger_one_day = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").withSchedule(SimpleScheduleBuilder.simpleSchedule().repeatForever().withIntervalInHours(24)).startAt(runTime_one_day).build();
            scheduler_one_day.scheduleJob(job_one_day, trigger_one_day);

            scheduler_one_day.start();
        } catch (SchedulerException e)
        {
            e.printStackTrace();
        }

        try
        {
            scheduler_one_hour = factory.getScheduler();
            Calendar c = Calendar.getInstance();
            c.set(Calendar.MINUTE, 30);
            c.set(Calendar.SECOND, 0);
            Date runTime_one_hour = c.getTime();
            if (runTime_one_hour.before(new Date()))
            {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(runTime_one_hour);
                calendar.add(Calendar.HOUR_OF_DAY, 1);
                runTime_one_hour = calendar.getTime();
            }

            // Date runTime_one_hour=DateBuilder.evenMinuteDateAfterNow();
            JobDetail job_one_hour = JobBuilder.newJob(BatchTaskJob_OneHour.class).withIdentity("job2", "group2").build();
            Trigger trigger_one_hour = TriggerBuilder.newTrigger().withIdentity("trigger2", "group2").withSchedule(SimpleScheduleBuilder.simpleSchedule().repeatForever().withIntervalInHours(1)).startAt(runTime_one_hour).build();
            scheduler_one_hour.scheduleJob(job_one_hour, trigger_one_hour);

            scheduler_one_hour.start();
        } catch (SchedulerException e)
        {
            e.printStackTrace();
        }

        try
        {
            scheduler_one_month = factory.getScheduler();

            JobDetail job_one_month = JobBuilder.newJob(BatchTaskJob_OneMonth.class).withIdentity("job3", "group3").build();
            // 每月1日上午5:00触发
            Trigger trigger_one_month = TriggerBuilder.newTrigger().withIdentity("trigger3", "group3").withSchedule(CronScheduleBuilder.cronSchedule("0 0 5 1 * ?")).build();

            scheduler_one_month.scheduleJob(job_one_month, trigger_one_month);

            scheduler_one_month.start();
        } catch (SchedulerException e)
        {
            e.printStackTrace();
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0)
    {
        // TODO Auto-generated method stub

    }

}
